#!/bin/bash

# Web Notes 服务器环境安装脚本
# 用法: ./install-dependencies.sh

set -e

echo "🔧 安装 Web Notes 应用依赖..."

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 检查是否为 CentOS 7
check_os() {
    if [[ ! -f /etc/redhat-release ]] || ! grep -q "CentOS Linux release 7" /etc/redhat-release; then
        echo -e "${RED}此脚本仅支持 CentOS 7${NC}"
        exit 1
    fi
}

# 更新系统
update_system() {
    echo -e "${YELLOW}更新系统包...${NC}"
    sudo yum update -y
    sudo yum install -y curl wget git vim epel-release
}

# 安装 Node.js 18
install_nodejs() {
    echo -e "${YELLOW}安装 Node.js 18...${NC}"
    
    if command -v node &> /dev/null; then
        echo -e "${GREEN}Node.js 已安装: $(node --version)${NC}"
        return
    fi
    
    # 添加 NodeSource 仓库
    curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
    
    # 安装 Node.js
    sudo yum install -y nodejs
    
    echo -e "${GREEN}Node.js 安装完成: $(node --version)${NC}"
    echo -e "${GREEN}npm 版本: $(npm --version)${NC}"
}

# 安装 MongoDB
install_mongodb() {
    echo -e "${YELLOW}安装 MongoDB...${NC}"
    
    if systemctl is-active --quiet mongod; then
        echo -e "${GREEN}MongoDB 已安装并运行${NC}"
        return
    fi
    
    # 创建 MongoDB 仓库
    sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo << 'EOF'
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF
    
    # 安装 MongoDB
    sudo yum install -y mongodb-org
    
    # 启动服务
    sudo systemctl start mongod
    sudo systemctl enable mongod
    
    echo -e "${GREEN}MongoDB 安装完成${NC}"
}

# 安装 Nginx
install_nginx() {
    echo -e "${YELLOW}安装 Nginx...${NC}"
    
    if systemctl is-active --quiet nginx; then
        echo -e "${GREEN}Nginx 已安装并运行${NC}"
        return
    fi
    
    # 安装 Nginx
    sudo yum install -y nginx
    
    # 启动服务
    sudo systemctl start nginx
    sudo systemctl enable nginx
    
    echo -e "${GREEN}Nginx 安装完成${NC}"
}

# 安装 PM2
install_pm2() {
    echo -e "${YELLOW}安装 PM2...${NC}"
    
    if command -v pm2 &> /dev/null; then
        echo -e "${GREEN}PM2 已安装: $(pm2 --version)${NC}"
        return
    fi
    
    # 安装 PM2
    sudo npm install -g pm2
    
    echo -e "${GREEN}PM2 安装完成${NC}"
    echo -e "${YELLOW}请运行以下命令设置 PM2 开机自启:${NC}"
    echo -e "${YELLOW}pm2 startup${NC}"
    echo -e "${YELLOW}然后运行显示的 sudo 命令${NC}"
}

# 配置防火墙
configure_firewall() {
    echo -e "${YELLOW}配置防火墙...${NC}"
    
    if ! systemctl is-active --quiet firewalld; then
        sudo systemctl start firewalld
        sudo systemctl enable firewalld
    fi
    
    # 开放端口
    sudo firewall-cmd --permanent --add-port=80/tcp
    sudo firewall-cmd --permanent --add-port=443/tcp
    sudo firewall-cmd --permanent --add-port=22/tcp
    sudo firewall-cmd --reload
    
    echo -e "${GREEN}防火墙配置完成${NC}"
}

# 优化系统
optimize_system() {
    echo -e "${YELLOW}优化系统配置...${NC}"
    
    # 增加文件描述符限制
    echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
    echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf
    
    # 优化网络参数
    echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf
    echo "net.ipv4.tcp_max_syn_backlog = 65535" | sudo tee -a /etc/sysctl.conf
    
    # 应用配置
    sudo sysctl -p
    
    echo -e "${GREEN}系统优化完成${NC}"
}

# 检查安装结果
check_installation() {
    echo -e "${YELLOW}检查安装结果...${NC}"
    
    echo "Node.js: $(node --version 2>/dev/null || echo '未安装')"
    echo "npm: $(npm --version 2>/dev/null || echo '未安装')"
    echo "PM2: $(pm2 --version 2>/dev/null || echo '未安装')"
    
    echo -e "\n服务状态:"
    echo "MongoDB: $(systemctl is-active mongod 2>/dev/null || echo '未运行')"
    echo "Nginx: $(systemctl is-active nginx 2>/dev/null || echo '未运行')"
    echo "防火墙: $(systemctl is-active firewalld 2>/dev/null || echo '未运行')"
    
    echo -e "\n开放端口:"
    sudo firewall-cmd --list-ports 2>/dev/null || echo "无法获取端口信息"
}

# 显示完成信息
show_completion() {
    echo -e "\n${GREEN}🎉 环境安装完成!${NC}"
    echo -e "\n${YELLOW}接下来的步骤:${NC}"
    echo -e "1. 设置 PM2 开机自启: ${YELLOW}pm2 startup${NC}"
    echo -e "2. 上传应用代码到服务器"
    echo -e "3. 运行部署脚本: ${YELLOW}./deploy.sh${NC}"
    
    echo -e "\n${YELLOW}有用的命令:${NC}"
    echo -e "查看服务状态: ${YELLOW}systemctl status nginx mongod${NC}"
    echo -e "查看防火墙端口: ${YELLOW}sudo firewall-cmd --list-ports${NC}"
    echo -e "查看系统资源: ${YELLOW}htop${NC} (需要安装: sudo yum install -y htop)"
}

# 主函数
main() {
    check_os
    update_system
    install_nodejs
    install_mongodb
    install_nginx
    install_pm2
    configure_firewall
    optimize_system
    check_installation
    show_completion
}

# 运行主函数
main "$@"